﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using EasyMvcBlog.CustomerEngine;
using EasyMvcBlog.DTO;
using EasyMvcBlog.Domain;
using EasyMvcBlog.Service;
using EasyMvcBlog.Filter;
using EasyMvcBlog.Data;
using Webdiyer.WebControls.Mvc;

namespace EasyMvcBlog.Controllers
{
    [Authorization]
    public class AdminController : BaseController
    {
        ArticleDomain articleDomain = new ArticleDomain();

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult ChangePassword(string oldPwd, string newPwd)
        {
            return Json(BlogDomain.ChangePwd(oldPwd, newPwd));
        }

        public ActionResult ChangePassword()
        {
            return View();
        }

        #region SoftwareStatistics

        public ActionResult SoftwareStatistics(int id = 1, string softwareName = "all")
        {
            SoftwareUseDomain domain = new SoftwareUseDomain();
            List<string> softwareNameList = domain.GetDistSoftwareName();
            softwareNameList.Insert(0, "all");

            ViewBag.SoftwareNameList = softwareNameList.ConvertAll<SelectListItem>(o => new SelectListItem() { Text = o, Value = o });
            ViewBag.TodayCount = domain.GetTodayUsage(softwareName);

            if (softwareName == "all")
            {
                return View(domain.GetSoftwareUseList(id, 10));
            }
            return View(domain.GetSoftwareUseList(id, 10, softwareName));
        }

        #endregion

        #region Article

        public ActionResult NewArticle()
        {
            var items = new List<SelectListItem>();
            CategoryService.GetAllCategory().ForEach(
                o => items.Add(new SelectListItem { Text = o.CategoryName, Value = o.Id.ToString() }));
            ViewBag.IsNew = true;
            ViewBag.CategorySelectList = items;
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult NewArticle(ArticleDTO article)
        {
            List<Tag> newTags = new List<Tag>();

            if (article.Tags != null)
            {
                foreach (var tag in article.Tags)
                {
                    if (tag.Id < 0)
                    {
                        tag.CreatedTime = DateTime.Now;
                        newTags.Add(tag);
                    }
                }
                TagDomain tagDomain = new TagDomain();
                tagDomain.AddTags(newTags);
            }
            if (string.IsNullOrEmpty(article.ShortDesc))
            {
                if (article.Content.Length <= 100)
                {
                    article.ShortDesc = article.Content;
                }
                else
                {
                    article.ShortDesc = article.Content.Substring(0, 100) + "......";
                }
            }

            if (article.Id == 0)
            {
                article.CreateTime = article.ChangedTime = DateTime.Now;
                articleDomain.CreateArticle(article);
                return RedirectToAction("EditArticle", new { id = article.Id });
            }
            else
            {
                article.ChangedTime = DateTime.Now;
                articleDomain.UpdateArticle(article);
                ShowMessage("更新成功", MessageType.success, 10000);
                return EditArticle(article.Id);
            }
        }

        public ActionResult Articles(int? id)
        {
            int pageIndex = 1;
            int pageSize = 5;
            if (id != null)
            {
                pageIndex = (int)id;
            }
            PagedArticleDTO dto = articleDomain.GetArticleByPage(pageIndex, pageSize);
            return View(dto);
        }

        public ActionResult EditArticle(int id)
        {
            var items = new List<SelectListItem>();
            CategoryService.GetAllCategory().ForEach(
                o => items.Add(new SelectListItem { Text = o.CategoryName, Value = o.Id.ToString() }));
            ViewBag.CategorySelectList = items;
            ViewBag.IsNew = false;

            ArticleDTO articleDTO = articleDomain.GetArticleDTO(id);
            return View("NewArticle", articleDTO);
        }

        #endregion

        #region Category

        public ActionResult Category()
        {
            CategoryDomain domain = new CategoryDomain();
            return View(domain.AllCategory);
        }

        public JsonResult EditCategory(int categoryId, string newName)
        {
            CategoryDomain domain = new CategoryDomain();
            if (domain.Update(categoryId, newName))
            {
                return Json("Success");
            }
            else
            {
                return Json("fail");
            }
        }

        [HttpPost]
        public ActionResult AddNewCategoryName(string newCategoryName)
        {
            CategoryDomain domain = new CategoryDomain();

            if (string.IsNullOrEmpty(newCategoryName))
            {
                ShowMessage("请输入分类名字", MessageType.error, 5000);
                return View("Category", domain.AllCategory);
            }

            Category newCategory = new Category()
            {
                CategoryName = newCategoryName,
                CreatedTime = DateTime.Now
            };
            if (domain.AddCategory(newCategory))
            {
                ShowMessage("添加成功", MessageType.success, 5000);
            }
            else
            {
                ShowMessage("添加失败", MessageType.error, 5000);
            }
            return View("Category", domain.AllCategory);
        }

        public JsonResult DeleteCategory(int categoryId)
        {
            CategoryDomain domain = new CategoryDomain();
            if (domain.Delete(categoryId))
            {
                return Json("success");
            }
            else
            {
                return Json("fail");
            }
        }

        #endregion

        #region Comment

        public JsonResult DeleteComment(int commentId)
        {
            CommentDomain commentDomain = new CommentDomain();
            if (commentDomain.Delete(commentId))
            {
                return Json("success");
            }
            else
            {
                return Json("fail");
            }
        }

        #endregion

        public ActionResult BlogSetting()
        {
            return View(BlogService.GetBlogInfo());
        }

        [HttpPost]
        public ActionResult BlogSetting(BlogDTO blog)
        {
            if (blog == null)
            {
                return View(BlogService.GetBlogInfo());
            }

            if (BlogDomain.UpdateBlogSetting(blog))
            {
                ShowMessage("修改成功", MessageType.success, 5000);
            }
            else
            {
                ShowMessage("修改失败", MessageType.error, 7000);                
            }

            return View(BlogService.GetBlogInfo());
        }


        public ActionResult BackupAndRestore()
        {
            return View();
        }
    }
}